﻿Public Class editProfile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If User.Identity.IsAuthenticated Then
            placeholder_loggedIn.Visible = True
        Else
            placeholder_notLoggedIn.Visible = True
        End If
        If Not IsPostBack Then
            Dim um As UserModel = New UserModel(User.Identity.Name)
            If um.IsSaved Then
                tb_FullName.Text = If(um.HasFullName, um.FullName, "")
                tb_Description.Text = If(um.HasDescription, um.Description, "")
                If um.HasPicture Then
                    img_profilePicture.ImageUrl = um.Picture
                    placeholder_profilePicture.Visible = True
                    placeholder_newProfilePicture.Visible = True
                Else
                    placeholder_noProfilePicture.Visible = True
                End If
            End If
        End If
    End Sub

    Protected Sub btn_SendProfile_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_SendProfile.Click
        Dim pictureUrl As String
        Dim um As UserModel = New UserModel(User.Identity.Name)
        um.FullName = tb_FullName.Text
        um.Description = tb_Description.Text
        If fup_Picture.HasFile Then
            pictureUrl = "/" & Guid.NewGuid().ToString() & ".jpg"
            fup_Picture.SaveAs(Request.MapPath("pictures") & pictureUrl)
            um.Picture = "~/pictures" & pictureUrl
        End If
        If Not (um.Save()) Then
            Throw New System.Exception("Uh-oh... something went wrong when adding or updating a user profile.")
        End If
        Response.Redirect("~/viewProfile.aspx?UserName=" & User.Identity.Name)
    End Sub
End Class